package com.shop.dingdongshoppingapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.dingdongshoppingapi.config.RedisKeyConfig;
import com.shop.dingdongshoppingapi.dao.SmsLogDao;
import com.shop.dingdongshoppingapi.dao.UserDao;
import com.shop.dingdongshoppingapi.dao.UserLogDao;
import com.shop.dingdongshoppingapi.entity.SmsLog;
import com.shop.dingdongshoppingapi.entity.User;
import com.shop.dingdongshoppingapi.service.intf.SmsLogService;
import com.shop.dingdongshoppingapi.type.SmsLogType;
import com.shop.dingdongshoppingapi.type.UserLogType;
import com.shop.dingdongshoppingapi.util.AliSmsUtil;
import com.shop.dingdongshoppingapi.util.JedisUtil;
import com.shop.dingdongshoppingapi.util.NumUtil;
import com.shop.dingdongshoppingapi.util.StringUtil;
import com.shop.dingdongshoppingapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SmsLogServiceImpl implements SmsLogService {
	@Resource
	private SmsLogDao smsLogDao;
	@Resource
	private UserDao userDao;
	@Resource
	private UserLogDao userLogDao;

	@Override
	public R sendRegisterCode(String phone) {
		if(StringUtil.isPhone(phone)){
			QueryWrapper<User> wrapper = new QueryWrapper<>();
			wrapper.eq("phone", phone);
			User user = userDao.selectOne(wrapper);
			if (user == null){
				String num = NumUtil.createCode(6)+"";
				if (AliSmsUtil.sendSmsCode(phone,num , AliSmsUtil.SMS_TEM_ADD)){
					JedisUtil.addString(RedisKeyConfig.SMS_CODE_ADD+phone, num, RedisKeyConfig.SMS_CODE_TIME);
					//记录日志
					smsLogDao.insert(new SmsLog(phone, UserLogType.注册.getValue(), 1,"注册验证码发送成功"));
					return R.ok();
				}else{
					smsLogDao.insert(new SmsLog(phone, UserLogType.注册.getValue(), 1,"注册验证码发送失败"));
					return R.fail("验证码发送失败,请联系客服");
				}
			}else{
				return R.fail("该账号已注册,请登录");
			}
		}
		return R.fail("请输入正确手机号");
	}

	@Override
	public R sendLoginCode(String phone) {
		if (StringUtil.isPhone(phone)) {
			//1.判断此号码是否注册
			User user = userDao.selectOne(new QueryWrapper<User>().eq("phone", phone));
			if (user != null) {
				//2.生成验证码
				String num = NumUtil.createCode(6) + "";
				//3.验证码发送是否成功
				if (AliSmsUtil.sendSmsCode(phone, num, AliSmsUtil.SMS_TEM_LOGIN)) {
					//4.记录验证码 到Redis
					JedisUtil.addString(RedisKeyConfig.SMS_CODE_LOGIN + user.getId(), num, RedisKeyConfig.SMS_CODE_TIME);
					//5.添加日志到信息发送日志表
					smsLogDao.insert(new SmsLog(phone, SmsLogType.登录验证码.getValue(), 1, "发送登录验证码成功"));
					return R.ok();
				} else {
					smsLogDao.insert(new SmsLog(phone, SmsLogType.登录验证码.getValue(), 1, "发送登录验证码失败"));
					return R.fail();
				}
			} else {
				return R.fail("亲,请您先注册,(*￣︶￣*)");
			}
		}
		return R.fail("请输入正确手机号");
	}

	@Override
	public R sendFindCode(String phone) {
		if (StringUtil.isPhone(phone)) {
			//判断此号码是否注册
			User user = userDao.selectOne(new QueryWrapper<User>().eq("phone", phone));
			if (user != null) {
				String num = NumUtil.createCode(6)+"";
				if (AliSmsUtil.sendSmsCode(phone, num, AliSmsUtil.SMS_TEM_FIND)){
					//存储验证码在jedis中
					JedisUtil.addString(RedisKeyConfig.SMS_CODE_FIND+phone, num, RedisKeyConfig.SMS_CODE_TIME);
					//添加日志
					smsLogDao.insert(new SmsLog(phone, SmsLogType.密码找回.getValue(), 1, "发送查找密码验证码成功"));
					return R.ok();
				}else{
					smsLogDao.insert(new SmsLog(phone, SmsLogType.密码找回.getValue(), 1, "发送查找密码验证码失败"));
					return R.fail();
				}
			}else{
				return R.fail("亲,请您先注册,(*￣︶￣*)");
			}
		}
		return null;
	}
}
